package com.scaffold.config;

import com.scaffold.client.impl.CaffeineCacheClient;
import com.scaffold.model.domain.UserDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * 初始化缓存
 *
 * @author Da Shuai
 * @since 2021-08-30 16:52:10
 */
@Component
@RequiredArgsConstructor
@Slf4j
public class InitCacheCommandLineRunner implements CommandLineRunner {

    private final CaffeineCacheClient caffeineCacheClient;

    @Override
    public void run(String... args) throws Exception {
        log.debug("初始化缓存---开始---");
        List<UserDO> userList = new ArrayList<>(5);
        for (int i = 0; i < 5; i++) {
            UserDO userDO = new UserDO();
            userDO.setId((long) i);
            userDO.setName("这是id为" + i + "的用户的姓名");
            userList.add(userDO);
        }
        for (UserDO userDO : userList) {
            caffeineCacheClient.put(userDO.getId().toString(), userDO);
        }
        log.debug("初始化缓存---结束---");
    }
}
